package com.welyo.view {
	import fl.motion.easing.Sine;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.URLRequest;
	import gs.easing.Expo;
	import gs.TweenLite;
	
	/**
	 * ...
	 * @author vu.doan
	 */
	public class PanContent extends Sprite {
		
		public static const OPEN_PAN	:String = "openPan";
		
		private var originalProp		:Rectangle;
		private var canOut				:Boolean;
		private var originalScale		:Object;
		
		public function PanContent() {
			mcBg.mouseEnabled = mcBg.mouseChildren = false;
		}
		
		public function set Props(rect:Rectangle):void {
			originalProp = rect;
		}
		
		public function set PropScale(value:Object):void {
			originalScale = value;
		}
		
		public function init(path:String):void {
			mcHit.buttonMode = true;
			mcHit.addEventListener(MouseEvent.CLICK, onClickPanHandler);
			mcHit.addEventListener(MouseEvent.MOUSE_OVER, onMouseOverPanHandler);
			mcHit.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutPanHandler);
			
			var ldr:Loader = new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteLoadImageHandler);
			ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErrorLoadImageHandler);
			ldr.load(new URLRequest(path));
		}
		
		public function over():void {
			if (!this.mouseEnabled) {
				return;
			}
			TweenLite.to(this, 0.5, {y:originalProp.y - 20});
		}
		
		public function out():void {
			if (!this.mouseEnabled) {
				return;
			}
			TweenLite.to(this, 0.5, {y:originalProp.y, ease:Expo.easeOut});
		}
		
		public function show(value:Number):void {
			this.mouseChildren = this.mouseEnabled = true;
			TweenLite.to(this, 0.5, {x:originalProp.x, y:originalProp.y, width:originalProp.width, height:originalProp.height, alpha:1, ease:Sine.easeOut});
		}
		
		public function hide(value:Number):void {
			this.mouseChildren = this.mouseEnabled = false;
			TweenLite.to(this, 0.5, {x:originalProp.x - 50, y:originalProp.y - 50, scaleX:0.4, scaleY:0.4, alpha:0, ease:Sine.easeOut});
		}
		
		private function onErrorLoadImageHandler(evt:IOErrorEvent):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadImageHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorLoadImageHandler);
		}
		
		private function onCompleteLoadImageHandler(evt:Event):void {
			evt.currentTarget.removeEventListener(Event.COMPLETE, onCompleteLoadImageHandler);
			evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, onErrorLoadImageHandler);
			
			var bmd:Bitmap = evt.currentTarget.content as Bitmap;
			bmd.smoothing = true;
			//if (bmd.width > mcHit.width) {
				//bmd.width = mcHit.width;
				//bmd.scaleY = mcHit.scaleX;
			//}
			//
			//if (bmd.height > mcHit.height) {
				//bmd.height = mcHit.height;
				//bmd.scaleX = bmd.scaleY;
			//}
			mcHolder.addChild(bmd);
			//bmd.x = (mcHit.width - bmd.width) / 2;
			//bmd.y = (mcHit.height - bmd.height) / 2;
		}
		
		private function onMouseOutPanHandler(evt:MouseEvent):void {
			out();
		}
		
		private function onMouseOverPanHandler(evt:MouseEvent):void {
			over();
		}
		
		private function onStartOverHandler():void {
			canOut = false;
		}
		
		private function onCompleteOverHandler():void {
			canOut = true;
		}
		
		private function onClickPanHandler(evt:MouseEvent):void {
			dispatchEvent(new Event(OPEN_PAN));
		}
	}
}